// pages/new/new.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

    addresData:{
      user:'',
      phone:'',
      sex:[{isSelect:true,title:'男'},{isSelect:false,title:'女'}],
      address:'请输入地址',
      datel:'',
      tag:[{isSelect:true,title:'公司'},{isSelect:false,title:'家'},{isSelect:false,title:'学校'}],
      default:0,
    },
    id:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if(options.id !== '0'){

      this.getAddressId(options.id)
      this.setData({id:options.id})

      wx.setNavigationBarTitle({
        title: '编辑地址'
      })

    }else{


      this.setData({

        addresData:{
          user:'',
          phone:'',
          sex:[{isSelect:true,title:'男'},{isSelect:false,title:'女'}],
          address:'请输入地址',
          datel:'',
          tag:[{isSelect:true,title:'公司'},{isSelect:false,title:'家'},{isSelect:false,title:'学校'}],
          default:0,
        }
      })
    }

  },
  toggleSex:function(e){


    this.toggleStatus(e,'sex')

  },
  toggleTag:function(e){

    this.toggleStatus(e,'tag')
  },
  toggleStatus:function(e,key){

    if(e.currentTarget.dataset.select){
      
      return
    }

    var addresData = this.data.addresData;

    for(var i = 0; i < addresData[key].length; i++){

      if(addresData[key][i].isSelect){

        addresData[key][i].isSelect = false;

        break;

      }

    }

    addresData[key][e.currentTarget.dataset.index].isSelect = true

    this.setData({
      addresData:addresData
    })



  },
  toggDefault:function(){

    this.data.addresData.default == 0? this.data.addresData.default=1:this.data.addresData.default=0

    this.setData({
      addresData:this.data.addresData
    })


  },
  modifyPhone:function(e){

    var phoneReg = /^1[3-9]\d{9}$/

    console.log("e==>",e)

    var phone = e.detail.value
   
    if(!phoneReg.test(phone)){

      wx.showToast({
        title: '手机号码不正确',
        icon:'none'
      })

      this.data.addresData.phone = ''

      this.setData({
        addresData:this.data.addresData
      })

    }else{

      this.data.addresData.phone = e.detail.value

      this.setData({

        addresData:this.data.addresData

      })
    }


  },
  modifyUser:function(e){

    this.data.addresData.user = e.detail.value

    this.setData({

      addresData:this.data.addresData

    })
  },
  modifyDatil:function(e){

    this.data.addresData.datel = e.detail.value

    this.setData({
      addresData:this.data.addresData
    })

  }
  ,
  modifyAddress:function(e){

    console.log('e==>',e)

    this.data.addresData.address = e.detail.value.join('')

    this.setData({
      addresData:this.data.addresData
    })

  },
  save:function(){

    var obj ={}

    var data = {
      user:{
        value:'',
        msg:'联系人不能为空'
      },
      phone:{
        value:'',
        msg:'手机号码不能为空'
      },
      address:{
        value:'请输入地址',
        msg:'地址不能为空'
      },
      datel:{
        value:'',
        msg:'门牌号不能为空'
      },
    }

    for(var key in data){

      if(data[key].value == this.data.addresData[key]){

        wx.showToast({
          title: data[key].msg,
          icon:'none'
        })

        return
      }

      obj[key]=this.data.addresData[key]

    }

    var key=['sex','tag']

    key.forEach(v => {
      
      for(var i = 0 ; i < this.data.addresData[v].length ; i++){


        if(this.data.addresData[v][i].isSelect){

          obj[v] = this.data.addresData[v][i].title

          break;
        }

      }

    });

    obj['default'] = this.data.addresData['default']


    wx.showLoading({
      title: '加载中...',
      icon:'none'
    })

    if(obj.default == 1){


      this.getDefaultAddress(obj)



    }else{

      if(this.data.id == 0){

        this.addAddress(obj)

      }else{

        this.updateAddress(this.data.id,obj)

      }
      

    }



    

  },
  //新增收货地址云函数
  addAddress:function(address){

    wx.cloud.callFunction({
      name:'add_address',
      data:address,
      success:res=>{

        wx.hideLoading()

        var obj ={
          user:'',
          phone:'',
          address:'请输入地址',
          default:0,
          datel:'',
          sex:[{isSelect:true,title:'男'},{isSelect:false,title:'女'}],
          tag:[{isSelect:true,title:'公司'},{isSelect:false,title:'家'},{isSelect:false,title:'学校'},] 
        }

        this.setData({
          addresData:obj
        })

        wx.navigateTo({
          url: '../address/address',
        })

      },
      fail:error=>{
        wx.hideLoading()
        console.log('云函数调用失败==>',error)
      }
    })

  },
  //获取默认收货地址
  getDefaultAddress:function(address){

    wx.cloud.callFunction({

      name:'get_default_address',

      success:res=>{
        
        console.log('res==>',res)

        if(res.result.data.length>0){

          this.updateDefaultAddress(address)

        }else{

          this.addAddress(address)

        }

      },
      fail:error=>{
       
        console.log('调用云函数失败==>',error)
      }
    })
  },
  //更新默认地址
  updateDefaultAddress:function (address) {
    
    wx.cloud.callFunction({
      name:'update_default_address',
      success:res=>{
        
        console.log('更新默认地址成功==>',res)

        if(this.data.id==0){
          this.addAddress(address)
        }else{
          this.updateAddress(this.data.id,address)
        }

        
        
      },
      fail:error=>{
       
        console.log('更新默认地址失败 error=>',error)
      }
    })

  },
  //通过id查询地址信息
  getAddressId:function(id){

    wx.showLoading({
      title: '加载中...',
    })
    console.log('id==>',id)
    wx.cloud.callFunction({
      name:'get_id_address',
      data:{id:id},
      success:res=>{

        wx.hideLoading()

        if(res.result.data[0].sex =="男"){

          res.result.data[0].sex =[{title:'男',isSelect:true},{title:'女',isSelect:false}]

        }else{

          res.result.data[0].sex =[{title:'男',isSelect:false},{title:'女',isSelect:true}]
        }

        var tag = [{title:'家',isSelect:false},{title:'公司',isSelect:false},{title:'学校',isSelect:false}]

        for(var i = 0 ; i < tag.length; i++){

          if(tag[i].title == res.result.data[0].tag){

            tag[i].isSelect = true

            res.result.data[0].tag = tag

            break;

          }

        }

        this.setData({
          addresData:res.result.data[0]
        })

      },
      fail:error=>{
        wx.hideLoading()
        console.log('云函数调用失败error=>',error)
      }
    })

  },
  //更新地址
  updateAddress:function(id,data){

    wx.cloud.callFunction({
      name:'update_address',
      data:{
        id:id,
        data:data
      },
      success:res=>{

        wx.hideLoading()
        console.log('更新成功==>',res)

        wx.navigateTo({
          url: '../address/address',
        })

      },
      fail:error=>{
        wx.hideLoading()
        console.log('云函数调用失败 error==>',error)
      }

    })





  }

})